package com.supermarket.api.web;

import com.supermarket.api.bean.user.LoginReq;
import com.supermarket.api.bean.user.LoginRes;
import com.supermarket.api.bean.user.RegisterReq;
import com.supermarket.api.bean.user.TokenData;
import com.supermarket.api.common.BaseResponse;
import com.supermarket.api.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 用户相关接口
 * @author 李其伟
 * @date 2023/10/29
 */
@Api(tags = "用户相关接口")
@Slf4j
@RestController
@RequestMapping("/user")
public class UserInfoController {

    @Autowired
    private UserInfoService userService;

    @ApiOperation(value = "用户注册", notes = " ")
    @PostMapping("/register")
    public String register(@Valid @RequestBody RegisterReq registerReq) {
        return BaseResponse.successJson(userService.register(registerReq));
    }

    @ApiOperation(value = "登录系统", notes = " ")
    @PostMapping ("/login")
    @ApiResponses({@ApiResponse(code=200, message = "", response = LoginRes.class)})
    public String login(@Valid @RequestBody LoginReq loginReq) {
        return BaseResponse.successJson(userService.login(loginReq));
    }

    @ApiOperation(value = "刷新token", notes = " ")
    @PostMapping ("/refresh")
    @ApiResponses({@ApiResponse(code=200, message = "", response = LoginRes.class)})
    public String refresh(@Valid @RequestBody TokenData tokenData) {
        return BaseResponse.successJson(userService.refresh(tokenData));
    }


}
